<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * Class CateController
 * @package Admin\Controller
 * 2018-04-11   菜品分类
 */
class CateController extends AdminBasicController{

    public $cate = '';
    public function _initialize(){
        $this->checkLogin();
        $this->cate = D('Cate');
    }

    /**
     * 分类列表
     * @author mss
     * @time 2018-04-11
     */
    public function cateList(){
        $where = array();
        $param = array();
        $where['status'] = array('NEQ',9);
        $order = 'sort DESC,ctime DESC';
        /**按名称搜索*/
        if(!empty(trim(I('request.title')))){
            $title = trim(I('request.title'));
            $where['cate_name'] = array('LIKE','%'.$title.'%');
            $param['title'] = $title;
        }

        /**按发布时间查找*/
        if(!empty(I('request.start_time')) && !empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $where['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }

        $list = $this->cate->selectCate($where,$order,15,$param);
        $data = $list['list'];

        $this->assign('list',$data);
        $this->assign('page',$list['page']);
        $this->assign('request',$_REQUEST);

        $this->display('cateList');
    }

    /**
     * 添加分类
     * @author mss
     * @time 2018-04-11
     */
    public function addCate(){
        if(IS_POST){
            $data = $this->cate->create();
            if($data){
                $data['ctime'] = time();
                $res = $this->cate->addCate($data);
                if($res){
                    $this->success('添加成功',U('Cate/cateList'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($this->cate->getError());
            }
        }else{

            $this->display('addCate');
        }

    }

    /**
     * 编辑分类
     * @author mss
     * @time 2018-04-11
     * @param cate_id 文章id
     */
    public function editCate(){
        if(IS_POST){
            $data = $this->cate->create();
            if($data){
                $where['cate_id'] = $_POST['cate_id'];
                $data['utime'] = time();
                $res = $this->cate->editCate($where,$data);
                if($res){
                    $this->success('编辑成功',U('Cate/cateList'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($this->cate->getError());
            }
        }else{
            $id = I('cate_id');
            $info = M('Cate')->where(array('cate_id'=>$id))->find();
            $this->assign('info',$info);
            $this->display('editCate');
        }
    }

    /**
     * 删除分类
     * @author mss
     * @time 2018-04-11
     */
    public function delCate(){
        if(empty($_REQUEST['cate_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['cate_id'] = array('IN',I('request.cate_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->cate->editCate($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

    /**
     * 修改分类显示顺序
     */
    public function ajaxUpCate(){
        $art_id = $_POST['cate_id'];
        $data['sort'] = $_POST['sort'];
        $data['utime'] = time();
        $where['cate_id'] = $art_id;
        $res = M('Cate')->where($where)->save($data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**
     * 修改分类显示状态
     * @author mss
     * @time 2018-04-11
     */
    public function ajaxStatus(){
        $id = $_POST['id'];
        $w['cate_id'] = $id;
        $data['status'] = $_POST['status'];
        $data['utime'] = time();
        $res = $this->cate->editCate($w,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**ajax上传图片*/
    public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/Cate/".date('Y-m-d').'/'.$ext;
        $saveRoot = "Uploads/Cate/".date('Y-m-d').'/';
        /**检查目录是否存在  循环创建目录*/
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
            $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
            $result_data['path'] = $pic_link;
            $ajaxData['data'] = $result_data;
            $this->ajaxReturn(json_encode($ajaxData));
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            $this->ajaxReturn(json_encode($ajaxData));
        }
    }

    /**删除上传的相册的图片*/
    public function delPhoto(){
        /**判断删除的商家的图片type:1头像 type2商家展示图 3商家的资质证明图*/

        $w['cate_id'] = $_POST['cate_id'];
        $res = M("Cate")->where($w)->limit(1)->find();
        $pic = "";
        if($_POST['type'] == 1){
            $data['pic'] = "";
            $pic = M("Cate")->where($w)->limit(1)->save($data);
        }else{
            $pic = 1;
        }

        $file  = $_POST['file_path'];
        $result = @unlink($file);
        if ($result == false && empty($pic)) {
            $this->ajaxReturn(0);
        } else {
            $this->ajaxReturn(1);
        }
    }

}